package tank;

import java.awt.Color;
import java.awt.Graphics;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import tank.cor.BullteTankCollider;
import tank.cor.Collider;
import tank.cor.ColliderChain;
import tank.cor.TankTankCollider;

public class GameModel {
	private static final GameModel INSTANCE = new GameModel();
	
	static {
		INSTANCE.init();
	}
	
	Tank myTank=null;
//	List<Bullet> bullets = new ArrayList<>();
//	List<Tank> tanks = new ArrayList<>();
//	List<Explode> explodes = new ArrayList<>();
	ColliderChain colliderChain = new ColliderChain();
	private List<GameObject> objects = new ArrayList<>();
	
	private GameModel() {
		
	}
	private void init() {
		myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD);
		int initTankCount = Integer.parseInt((String)PropertyMgr.get("initTankCount"));
		
		//可以通过配置文件的形式进行添加，对修改关闭，对扩展开发
		//初始化敌方坦克
		for(int i=0; i<initTankCount; i++) {
			new Tank(50 + i*80, 200, Dir.DOWN, Group.BAD);
		}
		add(new Wall(150,150,200,50));
		add(new Wall(550,150,200,50));
		add(new Wall(300,450,50,200));
		add(new Wall(500,450,50,200));
	}
	public static GameModel getInstance() {
		return INSTANCE;
	}
	public void add(GameObject go) {
		this.objects.add(go);
	}
	public void remove(GameObject go) {
		this.objects.remove(go);
	}
	public void paint(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.WHITE);
//		g.drawString("子弹的数量:" + bullets.size(), 10, 60);
//		g.drawString("敌人的数量:" + tanks.size(), 10, 80);
//		g.drawString("爆炸的数量:" + explodes.size(), 10, 100);
		g.setColor(c);

		myTank.paint(g);
		for (int i = 0; i < objects.size(); i++) {
			objects.get(i).paint(g);
		}
		//碰撞检测
		for(int i=0;i<objects.size();i++) {
			for(int j=i+1;j<objects.size();j++) {
				GameObject o1 = objects.get(i);
				GameObject o2 = objects.get(j);
//				bullteTankCollider.collide(o1, o2);
//				tankTankCollider.collide(o1, o2);
				colliderChain.collide(o1,o2);
			}
		}
		
	}
	public Tank getMainTank() {
		return myTank;
	}
	
	//保存当前游戏进度
	public void save() {
		File file = new File("D:/eclipseWorkSpace2/tank/tankData");
		ObjectOutputStream outputStream = null;
		try {
			outputStream  = new ObjectOutputStream(new FileOutputStream(file));
			outputStream.writeObject(myTank);
			outputStream.writeObject(objects);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	//还原当前游戏进度
	public void load() {
		File file = new File("D:/eclipseWorkSpace2/tank/tankData");
		ObjectInputStream inputStream = null;
		try {
			inputStream  = new ObjectInputStream(new FileInputStream(file));
			myTank = (Tank)inputStream.readObject();
			objects = (List<GameObject>)inputStream.readObject();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
